<template>
  <div class="import">
    <h2>导入{{ name }}</h2>
    <Readxlsx :on-success="onSuccess" />
  </div>
</template>
<script>
import { sysUserBatchPost } from '@/api/employees.js'
export default {
  name: '',
  data() {
    return {
      name: ''
    }
  },
  created() {
    this.name = this.$route.query.name
  },
  methods: {
    async onSuccess(obj) {
      // console.log(obj)
      const list = this.changeData(obj.results)
      console.log('list', list)
      await sysUserBatchPost(list)
      this.$message.success('导入成功')
      this.$router.push(this.$route.query.redirect || '/employees')
    },
    // 改变数据格式
    changeData(arr) {
      const obj = {
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      }
      const newArr = arr.map((item) => {
        const temp = {}
        Object.keys(item).forEach((item2) => {
          temp[obj[item2]] = item[item2]
          if (obj[item2] === 'timeOfEntry' || obj[item2] === 'correctionTime') {
            const t1 = new Date(
              (item[item2] - 1) * 24 * 60 * 60 * 1000 - 8 * 60 * 60 * 1000
            )
            t1.setYear(t1.getFullYear() - 70)
            temp[obj[item2]] = t1
          }
        })
        return temp
      })
      return newArr
    }
  }
}
</script>
<style scoped lang="scss">
.import {
  h2 {
    text-align: center;
  }
}
</style>
